import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import MyMessage from '../components/background/mycenter/MyMessage.vue'
import Manager from '../components/background/manager/manager.vue'
import Companylist from '../components/background/company/Companylist.vue'
import Career from '../components/background/career/Career.vue'
import Place from '../components/background/place/place.vue'
import Educationlist from '../components/background/education/Educationlist.vue'
import Carousellist from '../components/background/systemmanagement/Carousellist.vue'
import Recruit from '../components/background/recruit/Recruitlist.vue'
import Recruitadd from '../components/background/recruit/Recruitadd.vue'
import Apply from '../components/background/apply/Applylist.vue'
import JobHunting from '../components/background/jobhunting/JobHunting.vue'
import index from '../components/client/index.vue'
import Main from '../components/client/main/main.vue'
import UserRecruit from '../components/client/recruit/recruit.vue'
import Online from '../components/client/online/online.vue'
import UserCompany from '../components/client/company/company.vue'
import Register from '@/components/register.vue'
import Myself from '../components/client/myself/myself.vue'
import RecruitDetail from '../components/client/recruit/RecruitDetail.vue'
import MyApply from '../components/client/myself/myapply.vue'
import MyAttention from '../components/client/myself/myattention.vue'
import MyCollect from '../components/client/myself/mycollect.vue'
import Company from '../components/client/company/company.vue'
import CompanyDetail from '../components/client/company/companyDetail.vue'
//企业端
import ComHome from '../components/company/comHome.vue'
import ComWelcome from '../components/company/comWelcome.vue'
import ComInformation from '../components/company/comInformation/comInformation.vue'
import Jobseekers from '../components/company/Jobseekers/Jobseekers.vue'
import ComRecruit from '../components/company/comrecruit/comrecruit.vue'
Vue.use(VueRouter)

const router = new VueRouter({
    // routes,
    routes: [
        { path: '/', component: Login },
        { path: '/Login', component: Login },
        { path: '/Register', component: Register },
        { path: '/Recruitadd', component: Recruitadd },
        {
            path: '/Home',
            component: Home,
            redirect: 'Welcome',
            children: [
                { path: '/Welcome', component: Welcome },
                { path: '/MyMessage', component: MyMessage },
                { path: '/Manager', component: Manager },
                { path: '/Companylist', component: Companylist },
                { path: '/Career', component: Career },
                { path: '/Place', component: Place },
                { path: '/Educationlist', component: Educationlist },
                { path: '/Carousellist', component: Carousellist },
                { path: '/Recruit', component: Recruit },
                { path: '/Apply', component: Apply },
                { path: '/JobHunting', component: JobHunting }



            ]
        },
        {
            path: '/index',
            component: index,
            redirect: '/main',
            children: [
                { path: '/main', component: Main },
                { path: '/userRecruit', component: UserRecruit },
                { path: '/online', component: Online },
                { path: '/userCompany', component: UserCompany },
                {
                    path: '/myself',
                    component: Myself,
                    children: [
                        { path: '/MyApply', component: MyApply },
                        { path: '/MyAttention', component: MyAttention },
                        { path: '/MyCollect', component: MyCollect }

                    ]
                },
                { path: '/RecruitDetail', component: RecruitDetail },
                { path: '/Company', component: Company },
                { path: '/CompanyDetail', component: CompanyDetail }


            ]
        },
        {
            path: '/ComHome',
            component: ComHome,
            redirect: 'ComWelcome',
            children: [
                { path: '/ComWelcome', component: ComWelcome },
                { path: '/ComInformation', component: ComInformation },
                { path: '/Jobseekers', component: Jobseekers },
                { path: '/ComRecruit', component: ComRecruit }

            ]
        }


    ]
})
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(to) {
        return VueRouterPush.call(this, to).catch(err => err)
    }
    // 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // to 将要访问的路径
    // from 代表从哪个路径跳转而来
    // next 是一个函数，表示放行
    //     next()  放行    next('/login')  强制跳转

    if (to.path === '/login') return next()
    if (to.path === '/Register') return next()
        // 获取token
    const tokenStr = window.sessionStorage.getItem('token')
    if (!tokenStr) return next('/login')
    window.scrollTo(0, 0)
    next()
})

export default router